import router from './router';
import NProgress from 'nprogress'; // progress bar
import 'nprogress/nprogress.css'; // progress bar style
import { getToken } from '@/utils/auth'; // get token from cookie

NProgress.configure({ showSpinner: false }); // NProgress Configuration

const whiteList = [
    '/login',
    '/auth-redirect',
    '/temporary/payment',
    '/temporary/labourAdministration',
    '/temporary/yidiant',
    '/temporary/jklxf'
]; // no redirect whitelist

router.beforeEach(async (to, from, next) => {
    // 进度条
    NProgress.start();
    // 获取token
    const hasToken = getToken();
    if (hasToken) {
        if (to.path === '/login') {
            // 如果已登录，请重定向到主页
            next({ path: '/' });
        } else {
            next();
        }
    } else {
        /** 没有登录Token 跳转到 登录页**/
        if (whiteList.indexOf(to.path) !== -1) {
            next();
        } else {
            // 其他没有访问权限的页面将重定向到登录页面。
            next(`/login?redirect=${to.path}`);
        }
    }
});

router.afterEach(() => {
    // finish progress bar
    NProgress.done();
});
